<?php

ini_set('include_path', '../');

require_once("lib/exception/internal_error_exception.php");
require_once("lib/exception/model_exception.php");
require_once("lib/exception/parameters_exception.php");
require_once("lib/exception/privileges_exception.php");

require_once("util/configuration_parametters.php");

require_once("model/autor/dao/autor_dao_factory.php");
require_once("model/article_comment/dao/article_comment_dao_factory.php");

require_once("model/autor/to/autor_to.php");
require_once("model/article/to/article_to.php");
require_once("model/article_category/to/article_category_to.php");
require_once("model/article_image/to/article_image_to.php");
require_once("model/article_link/to/article_link_to.php");
require_once("model/article_description/to/article_description_to.php");
require_once("model/article_comment/to/article_comment_to.php");

require_once("model/articlefacade/delegate/article_facade_delegate_factory.php");



class Articlefacade_test {
	
	private $dao;
	private $autor1;
	private $autor2;
	
	public function before() {
		try {
			$this->dao = Autor_dao_factory::singleton()->get_dao();
			$this->autor1 = new Autor_to('xxxxxxxxxxxx','password','email');
			$this->autor1 = $this->dao->create($this->autor1);
			$this->autor2 = new Autor_to('yyyyyyyyyyyy','password','email2');
			$this->autor2 = $this->dao->create($this->autor2);
		}
		catch (Exception $e) {
			throw $e;
		}
		
	}

	public function run() {
		try {
		
			$article_facade = Article_facade_delegate_factory::singleton()->get_delegate();
			
			$date = date("Y-m-d H:i:s");
			
			//crea un articulo
			$article_to = (object) new Article_to(0, 'title', $this->autor1->get_login(), $date, 'open', 0);	
			$article_to = $article_facade->create_article($article_to);
			echo 'create article: ' . $article_to->to_string() . '</br>';
			
			//Intenta crear un articulo con el mismo titulo del mismo autor
			try {
				$article_facade->create_article($article_to);
			} catch (Model_exception $e) {
				echo 'Correct error to create article: ' . $article_to->to_string() . '</br>';
			}
			
			//Intenta crear un articulo con el mismo titulo de distinto autor
			$aux_article_to = (object) new Article_to(0, 'title', $this->autor2->get_login(), $date, 'open', 0);	
			$aux_article_to = $article_facade->create_article($aux_article_to);
			echo 'create article: ' . $aux_article_to->to_string() . '</br>';
			
			//Crea una categoria
			$article_category_to = new Article_category_to(0,'title', 'description', $this->autor1->get_login(), 0);
			$article_category_to = $article_facade->create_category($article_category_to);
			echo 'create article_category: ' . $article_category_to->to_string() . '</br>';
			
			//crea una categoria con el mismo itulo
			try {
				$article_facade->create_category($article_category_to);
			} catch (Model_exception $e) {
				echo 'Correct error to create category: ' . $article_category_to->to_string() . '</br>';
			}
			
			if (!$article_facade->exist_category_by_title('xxxx'))
				echo 'Not exist category by title: xxxx</br>';
			else
				throw new Exception('Exist category inexistent: xxxx');
			
			if ($article_facade->exist_category_by_title($article_category_to->get_title()))
				echo 'Exist category by title: ' . $article_category_to->to_string() . '</br>';
			else
				throw new Exception('Not exist category existent: ' . $article_category_to->get_title());
			
			//Create category 2					
			$article_category_to2 = new Article_category_to(0,'title2', 'description', $this->autor1->get_login(), 0);
			$article_category_to2 = $article_facade->create_category($article_category_to2);
			echo 'create article_category: ' . $article_category_to->to_string() . '</br>';
			
			//Asign categories to article
			$category_list[0] = $article_category_to;
			$category_list[1] = $article_category_to2;
			$article_facade->create_article_category($article_to, $category_list);
			echo 'create article_article_category: ' . $article_to->get_number() . ' | ';
			echo $article_category_to->get_number() . ' , ' . $article_category_to2->get_number() . '</br>';
			
			//Obtain all categories for article inexistent
			try {
				$category_list = $article_facade->find_categories_by_article(9999);
			}
			catch (Model_exception $e) {
				echo 'Not found categories for article 9999</br>';
			}
						
			//Obtain all categories for article existent and add more
			$category_list = $article_facade->find_categories_by_article($article_to->get_number());
				echo 'Categories for article: ' . $article_to->to_string() . '</br>';
				foreach($category_list as $category)
					echo '   category: ' . $category->to_string() . '</br>';
				
			//Obtain all categories for article existent and add more
			$category_list = $article_facade->find_categories_by_article($article_to->get_number());
				$article_category_to3 = new Article_category_to(0,'title3', 'description', $this->autor2->get_login(), 0);
				$article_category_to3 = $article_facade->create_category($article_category_to3);
				$category_list[count($category_list)] = $article_category_to3;
				$article_facade->create_article_category($article_to, $category_list);		
				echo 'Add new category to article ' . $article_to->get_number() . ': ' . $article_category_to3->to_string() . '</br>';	
				
			//Obtain all categories for article existent
			$category_list = $article_facade->find_categories_by_article($article_to->get_number());
				echo 'Categories for article: ' . $article_to->to_string() . '</br>';
				foreach($category_list as $category)
					echo '   category: ' . $category->to_string() . '</br>';

			//Remove category by unauthorized user
			try {
				$article_facade->remove_category_for_article($article_to->get_number(), 
																$article_category_to3->get_number(),
																$this->autor1->get_login());
				throw new Exception('Deleted category from unauthorized user');
			}
			catch (Privileges_exception $e) {
				echo 'Correct error to delete catgory by unauthorized user</br>';
			}
			
			//Remove category by authorized user
			$article_facade->remove_category_for_article($article_to->get_number(), 
																$article_category_to3->get_number(),
																$this->autor2->get_login());
			echo 'Deleted category by authorized user:</br>';				
			
			//Obtain all categories for article existent
			$category_list = $article_facade->find_categories_by_article($article_to->get_number());
				echo 'Categories for article: ' . $article_to->to_string() . '</br>';
				foreach($category_list as $category) 
					echo '   category: ' . $category->to_string() . '</br>';
			
			//Create image
			$article_image_to = new Article_image_to(0, 'link', 'description', $article_to->get_number(), 
														$this->autor1->get_login(), $date, 'open');
			$article_image_to = $article_facade->create_image($article_image_to);
			echo 'Create image: ' . $article_image_to->to_string() . '</br>';			
			
			//Create image2
			$article_image_to2 = new Article_image_to(0, 'link2', 'description', $article_to->get_number(), 
														$this->autor2->get_login(), $date, 'open');
			$article_image_to2 = $article_facade->create_image($article_image_to2);
			
			//find images by article
			$image_list = $article_facade->find_images_by_article($article_to->get_number());
				echo 'Found images for article: ' . $article_to->to_string() . '</br>';
				foreach ($image_list as $image) 
					echo 'Image: ' . $image->to_string() . '</br>';
				
			//Remove image for unauthorized user
			try {
				$article_facade->remove_image($article_image_to->get_number(), $this->autor2->get_login());
				throw new Exception('Deleted image from unauthorized user');
			}
			catch (Privileges_exception $e) {
				echo 'Correct error to delete image for unauthorized user</br>';
			}
			
			//Remove for authorized user
			$article_facade->remove_image($article_image_to->get_number(), $this->autor1->get_login());
			echo 'Deleted image by authorized user</br>';
					
			//Create video
			$article_video_to = new Article_video_to(0, 'link', 'description', $article_to->get_number(), 
														$this->autor1->get_login(), $date, 'open');
			$article_video_to = $article_facade->create_video($article_video_to);
			echo 'Create video: ' . $article_video_to->to_string() . '</br>';			
			
			//Create video2
			$article_video_to2 = new Article_video_to(0, 'link2', 'description', $article_to->get_number(), 
														$this->autor2->get_login(), $date, 'open');
			$article_video_to2 = $article_facade->create_video($article_video_to2);
			
			//find videos by article
			$video_list = $article_facade->find_videos_by_article($article_to->get_number());
				echo 'Found videos for article: ' . $article_to->to_string() . '</br>';
				foreach ($video_list as $video) 
					echo 'video: ' . $video->to_string() . '</br>';
				
			//Remove video for unauthorized user
			try {
				$article_facade->remove_video($article_video_to->get_number(), $this->autor2->get_login());
				throw new Exception('Deleted video from unauthorized user');
			}
			catch (Privileges_exception $e) {
				echo 'Correct error to delete video for unauthorized user</br>';
			}
			
			//Remove for authorized user
			$article_facade->remove_video($article_video_to->get_number(), $this->autor1->get_login());
			echo 'Deleted video by authorized user</br>';
			
			//Create link
			$article_link_to = new Article_link_to(0, 'link', 'description', 2, 0, $article_to->get_number(), 
														$this->autor1->get_login(), $date, 'open');
			$article_link_to = $article_facade->create_link($article_link_to);
			echo 'Create link: ' . $article_link_to->to_string() . '</br>';			
			
			//Create link2
			$article_link_to2 = new Article_link_to(0, 'link2', 'description', 2, 0, $article_to->get_number(), 
														$this->autor2->get_login(), $date, 'open');
			$article_link_to2 = $article_facade->create_link($article_link_to2);
			
			//find links by article
			$link_list = $article_facade->find_links_by_article($article_to->get_number());
				echo 'Found links for article: ' . $article_to->to_string() . '</br>';
				foreach ($link_list as $link) 
					echo 'link: ' . $link->to_string() . '</br>';
				
			//Remove link for unauthorized user
			try {
				$article_facade->remove_link($article_link_to->get_number(), $this->autor2->get_login());
				throw new Exception('Deleted link from unauthorized user');
			}
			catch (Privileges_exception $e) {
				echo 'Correct error to delete link for unauthorized user</br>';
			}
			
			//Remove for authorized user
			$article_facade->remove_link($article_link_to->get_number(), $this->autor1->get_login());
			echo 'Deleted link by authorized user</br>';
			
			//Create description
			$article_description_to = new Article_description_to(0, 'content', $article_to->get_number(), 
														$this->autor1->get_login(), $date, 'open');
			$article_description_to = $article_facade->create_description($article_description_to);
			echo 'Create description: ' . $article_description_to->to_string() . '</br>';			
			
			//Create description2
			$article_description_to2 = new Article_description_to(0, 'titcontent2', $aux_article_to->get_number(), 
														$this->autor2->get_login(), $date, 'open');
			$article_description_to2 = $article_facade->create_description($article_description_to2);
			
			//find descriptions by article
			$description_list = $article_facade->find_descriptions_by_article($article_to->get_number());
				echo 'Found descriptions for article: ' . $article_to->to_string() . '</br>';
				foreach ($description_list as $description) 
					echo 'description: ' . $description->to_string() . '</br>';
				
			//Remove description for unauthorized user
			try {
				$article_facade->remove_description($article_description_to->get_number(), $this->autor2->get_login());
				throw new Exception('Deleted description from unauthorized user');
			}
			catch (Privileges_exception $e) {
				echo 'Correct error to delete description for unauthorized user</br>';
			}
			
			//Remove for authorized user
			$article_facade->remove_description($article_description_to->get_number(), $this->autor1->get_login());
			echo 'Deleted description by authorized user</br>';
			
			//Create comment
			$article_comment_to = new Article_comment_to(0, 'title', 'content', $article_to->get_number(), 
														$this->autor1->get_login(), $date, 'open');
			$article_comment_to = $article_facade->create_comment($article_comment_to);
			echo 'Create comment: ' . $article_comment_to->to_string() . '</br>';			
			
			//Remove comment for unauthorized user
			try {
				$article_facade->remove_comment($article_comment_to->get_number(), $this->autor2->get_login());
				throw new Exception('Deleted comment from unauthorized user');
			}
			catch (Privileges_exception $e) {
				echo 'Correct error to delete comment for unauthorized user</br>';
			}
			
			//Remove for authorized user
			$article_facade->remove_comment($article_comment_to->get_number(), $this->autor1->get_login());
			echo 'Deleted comment by authorized user</br>';
						
//			//Find most importants categories
//			$category_list = $article_facade->find_most_importants_categories();
//			if (!is_null($category_list)) {
//				echo 'Most importants categories:</br>';
//				foreach ($category_list as $category)
//					echo 'Categoria: ' . $category->to_string() . '</br>';
//			}
//			else
//				throw new Exception('Not found importants categories');
			
			//Find all categories
			$category_list = $article_facade->find_all_categories();
				echo 'All categories:</br>';
				foreach ($category_list as $category)
					echo 'Categoria: ' . $category->to_string() . '</br>';

			//Find most recent articles
			$article_chunk = $article_facade->find_most_recent_articles(0, 1);
			echo 'Most recent articles</br>';
			echo $article_chunk->to_string() . '</br>';
				
			//Find most viewed articles
			$article_chunk = $article_facade->find_most_viewed_articles(0, 2);
			echo 'Most view articles</br>';
			echo $article_chunk->to_string() . '</br>';			
			
			//Get article
			$article = $article_facade->get_basic_article($article_to->get_number());
			echo 'Get basic article: ' . $article->to_string() . '</br>';
			
			//Find article by keywords
			$article_facade->create_article_category($aux_article_to, $category_list);
			$keywords[0] = 'tit';
			$keywords[1] = '2';
			$where[0] = 'description';
			//$where[0] = 'category';
			//$where = NULL;
			$type = 'and';
			$article_chunk = $article_facade->find_articles_by_keywords($where, $type, $keywords, 0, 2);
			echo 'Find articles by keyword:</br>';
			echo 'article: ' . $article_chunk->to_string() . '</br>';
			
			$aux_dao = Article_comment_dao_factory::singleton()->get_dao();
			$article_comment_to = new Article_comment_to(0, 'title', 'content', $article_to->get_number(), 
									$this->autor1->get_login(), $date, 'open');
			$date = date("Y-m-d H:i:s");
			$article_comment_to = new Article_comment_to(0, 'title', 'content', $article_to->get_number(), 
									$this->autor1->get_login(), $date, 'open');
									
			//find comments by article number
			$comments = $article_facade->find_comments_by_article($article_to->get_number());
				echo 'Find comments by article: </br>';
				foreach($comments as $comment) 
					echo 'comment: ' . $comment->to_string() . '</br>';
			
			
			//Get article
			$article = $article_facade->get_full_article($article_to->get_number());
			echo 'Get full article: ' . $article->to_string() . '</br>';
										
		}
		catch (Internal_error_exception $e) {
			throw $e;
		}
		catch (Model_exception $e) {
			throw $e;
		}
		catch (Parameters_exception $e) {
			throw $e;
		}
		catch (Exception $e) {
			throw new Exception("Internal error exception --> " . $e);
		}
	}
	
	public function after() {
		try {
			$this->dao->remove($this->autor1->get_login());
			$this->dao->remove($this->autor2->get_login());
		}
		catch (Exception $e) {
			throw $e;
		}
	}
}

$test = new Articlefacade_test();

try {
	$test->before();
	$test->run();
	$test->after();
}
catch (Exception $e) {
	echo $e->getMessage();
}
?>